import {
	RouterMount,
	createRouter,
	runtimeQuit
} from 'uni-simple-router';
import store from '@/store';

const router = createRouter({
	platform: process.env.VUE_APP_PLATFORM,
	// 路由锁
	applet: {
		animationDuration: 300 // 默认300ms
		// animationDuration: 0 // 不精准 只捕捉跳转api下的complete函数
	},
	routerErrorEach: ({
		type,
		msg
	}) => {
		console.log({
			type,
			msg
		})
		//解决app及小程序使用beforeRouteLeave拦截后允许跳转不跳页
		if (type === 0) {
			//	解锁
			router.$lockStatus = false;
		}
	},
	routes: [...ROUTES]
});

//全局路由前置守卫
router.beforeEach((to, from, next) => {
	if (to.meta.loginAuth) {
		// #ifdef H5
		if (process.env.VUE_APP_H5_REGISTER) {
			uni.reLaunch({
				url: '/register'
			});
			return
		}
		// #endif
		const token = store.state.user.token
		if (token) {
			next()
		} else {
			// next()
			next({
				// path: "/login",
				name: process.env.VUE_APP_REDIRECT,
				NAVTYPE: 'replaceAll',
				query: {
					redirect: to.aliasPath,
					NAVTYPE: to.meta.NAVTYPE,
					params: to.query
				}
			});
		}
	} else {
		next()
	}
});
// 全局路由后置守卫
router.afterEach((to, from) => {
	// console.log('跳转结束')
})

export {
	router,
	RouterMount
}
